package 面试经典150题.双指针;

/**
 * @author tmh
 * @date 2024/6/8 20:12
 * @description 因为题目是非递减顺序，而且数组里面也只有两个数满足要求，就是如下的代码，链接的这个解释很妙
 * https://leetcode.cn/problems/two-sum-ii-input-array-is-sorted/solutions/87919/yi-zhang-tu-gao-su-ni-on-de-shuang-zhi-zhen-jie-fa/?envType=study-plan-v2&envId=top-interview-150
 */
public class T167两数之和II输入有序数组 {
    public int[] twoSum(int[] numbers, int target) {
        int i = 0;
        int j = numbers.length - 1;
        while (i < j) {
            int sum = numbers[i]+numbers[j];
            if(sum>target){
                j--;
            }else if(sum<target){
                i++;
            }else {
                return new int[]{i+1,j+1};
            }
        }
        return new int[]{-1,-1};
    }
}
